﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; //xxxxxxx
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Vision;
using AForge.Vision.Motion;

namespace MotionDetection
{
    public partial class Form1 : Form
    {
        public FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        public VideoCaptureDevice videoCaptureDevice;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (FilterInfo device in videoDevices)
            {
                comboBox1.Items.Add(device.Name);
                comboBox1.SelectedItem = device.Name;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            videoCaptureDevice = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoCaptureDevice.NewFrame += new NewFrameEventHandler(videoCaptureDevice_NewFrame);
            // start the video source
            videoCaptureDevice.Start();

        }

        void videoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap image = (Bitmap)eventArgs.Frame.Clone();
            if (radioButton1.Checked)
            {
                //ColorFilter'in yapildigi yer
                YCbCrFiltering filter = new YCbCrFiltering();
                // Algilanacak renkler belirleniyor
                filter.Cb = new Range(-0.5f, -0.04f);
                filter.Cr = new Range(0.02f, 0.5f);
                // apply the filter
                filter.ApplyInPlace(image);
                //Görüntü üzerinde algilanan renk kare icine aliniyor
                cevresiniciz(image);
            }
            if (radioButton2.Checked)
            {
                // create filter
                EuclideanColorFiltering filter = new EuclideanColorFiltering();
                // Algilanacak renk belirleniyor ve orta noktasi belirleniyor
                filter.CenterColor = new RGB(Color.FromArgb(195, 19, 56));
                filter.Radius = 100;
                // filtre calistiriliyor
                filter.ApplyInPlace(image);
                //Görüntü üzerinde algilanan rengi cevrcevelemek veya hedeflemek icin gerekli Methodlar
                cevresiniciz(image);
                //hedefal(image);
            }
            pictureBox1.Image = image;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoCaptureDevice.IsRunning)
                videoCaptureDevice.Stop();
        }
        public void cevresiniciz(Bitmap image)
        {
            BlobCounter blobCounter = new BlobCounter();
            blobCounter.MinWidth = 2;
            blobCounter.MinHeight = 2;
            blobCounter.FilterBlobs = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;
            //Bitmap grayImage = grayFilter.Apply(image);
            blobCounter.ProcessImage(image);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            foreach (Rectangle recs in rects)
            {
                if (rects.Length > 0)
                {
                    Rectangle objectRect = rects[0];
                    //Graphics g = Graphics.FromImage(image);
                    Graphics g = pictureBox1.CreateGraphics();
                    using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                    {
                        g.DrawRectangle(pen, objectRect);
                    }
                    g.Dispose();
                }
            }
        }
    }
}
